<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>机蜂博客</title>
    <base href="<%=request.getContextPath()%>/">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <script src="static/jquery/jquery.js"></script>
    <script src="static/jquery/js.cookie.js"></script>
    <script src="static/layui/layui.js"></script>
</head>
<body>
<div style="padding: 20px">
    <form class="layui-form" id="forgetForm">
        <div class="layui-form-item">
            <div class="layui-row">
                <div class="layui-col-xs7">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-cellphone"></i>
                        </div>
                        <input type="text" name="phone" value="" lay-verify="required|phone"
                               placeholder="手机号" lay-reqtext="请填写手机号" autocomplete="off"
                               class="layui-input" id="regPhone">
                    </div>
                </div>
                <div class="layui-col-xs5">
                    <div style="margin-left: 11px;">
                        <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary"
                                id="sendMsg">获取验证码
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-vercode"></i>
                </div>
                <input type="text" name="code" value="" lay-verify="required" placeholder="验证码"
                       lay-reqtext="请填写验证码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="password" value="" lay-verify="required" placeholder="密码"
                       autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="confirmPassword" value=""
                       lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off"
                       class="layui-input" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="saveBtn">重置密码</button>
        </div>
    </form>
</div>
</body>
<script src="static/common/js/ajax.js"></script>
<script src="static/common/js/base.js"></script>
<script>
    let element;
    let codeUUID;//短信验证码uuid
    layui.use(['form', 'element'], function () {
        const form = layui.form;
        element = layui.element;
        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function (value) {
                var passwordValue = $('#reg-password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                }
            }
        });
        /*监听注册按钮*/
        form.on('submit(saveBtn)', function (data) {
            data.field.uuid = codeUUID;
            http.post('blog/reset', data.field, function () {
                layer.alert('修改密码成功！', {icon: 6}, function () {
                    /*获取当前窗口的标记值*/
                    const index = parent.layer.getFrameIndex(window.name);
                    /*关闭窗口*/
                    parent.layer.close(index);
                });
            });
            return false;
        });

        /*发送短信按钮*/
        $("#sendMsg").click(function () {
            const phone = $("#regPhone").val();
            if (!phone) {
                layer.msg('请输入手机号码!', {icon: 2});
                return false;
            }
            if (!(/^1\d{10}$/.test(phone))) {
                layer.msg('手机号码格式错误!', {icon: 2});
                return false;
            }
            $(this).addClass('layui-btn-disabled');
            let s = 59;
            $(this).text(s + "秒后重试...");
            let inter = setInterval(() => {
                s--;
                if (s === 0) {
                    $(this).removeClass("layui-btn-disabled").text("获取验证码")
                    clearInterval(inter);
                } else {
                    $(this).text((s < 10 ? '0' + s : s) + "秒后重试...");
                }

            }, 1000);
            http.get('blog/send', {phone}, function (res) {
                layer.msg('发送成功！', {icon: 6});
                codeUUID = res.msg
            })
        });
    })
</script>
</html>
